using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using Spine.Unity;
using System;

public class UIGold : MonoBehaviour
{
    [HideInInspector]
    public RectTransform rect;

    public RectTransform BG;

    public Image GoldPrefab;

    public Image TargetGoldImage; //目标点

    private List<Image> GoldPrefabLists = new List<Image>();
    public List<Sprite> numberSprites;

    public GameObject ImageParent;

    public Image Number1;

    public Image Number2;
    public Image Number3;

    public SkeletonGraphic skeletonGraphic;

    public Ease easeType;

    public Image Effect;

    public List<RectTransform> BallGolds = new List<RectTransform>();

    public Vector2 offset;

    void Awake()
    {
        rect = GetComponent<RectTransform>();
    }
    // Start is called before the first frame update
    void Start()
    {
        EventManager.Instance.Subscribe(ScreenChangeEvent.EventId, ScreenChangeCallBack);
        ScreenChangeCallBack(null, null);
    }

    // Update is called once per frame
    async void Update()
    {
        for (int i = 0; i < BallGolds.Count; i++)
        {
            Vector2 pos = UpdateRectPosition(BiliardGameManager.instance.targetBalls[i].transform.position, offset);
            if (BallGolds[i].anchoredPosition != pos)
            {
                BallGolds[i].anchoredPosition = pos;
            }
        }
    }

    public void PlaySkeletonGraphic(int index)
    {
        switch (index)
        {
            case 1:
                skeletonGraphic.gameObject.transform.localScale = new Vector3(1, 1, 1);
                break;
            case 2:
                skeletonGraphic.gameObject.transform.localScale = new Vector3(2, 2, 2);
                break;
            case 3:
                skeletonGraphic.gameObject.transform.localScale = new Vector3(3, 3, 3);
                break;
            case 4:
                skeletonGraphic.gameObject.transform.localScale = new Vector3(4, 4, 4);
                break;
        }
        skeletonGraphic.gameObject.SetActive(true);
        skeletonGraphic.rectTransform.anchoredPosition = UpdateRectPosition(BiliardGameManager.instance.baxin.transform.position, Vector2.zero);
        skeletonGraphic.gameObject.SetActive(true);
        skeletonGraphic.AnimationState.ClearTracks();
        skeletonGraphic.Skeleton.SetToSetupPose();
        skeletonGraphic.AnimationState.SetAnimation(0, "animation", false);
    }


    public async void GetGold(GameObject go)
    {
        Image goldImage = null;
        if (GoldPrefabLists.Count != 0)
        {
            goldImage = GoldPrefabLists[0];
            GoldPrefabLists.RemoveAt(0);
        }
        else
        {
            goldImage = Instantiate(GoldPrefab).GetComponent<Image>();
            goldImage.transform.SetParent(ImageParent.transform);
            goldImage.transform.localScale = Vector3.one;
        }

        goldImage.gameObject.SetActive(true);
        Vector2 targetpos = UpdateRectPosition(go.transform.position, Vector2.zero);
        goldImage.rectTransform.anchoredPosition = targetpos;
        goldImage.rectTransform.sizeDelta = new Vector2(139, 119);

        goldImage.rectTransform.anchoredPosition = UpdateRectPosition(go.transform.position, Vector2.zero);


        int randomvalueX = UnityEngine.Random.Range(-150, 150);
        int randomvalueY = UnityEngine.Random.Range(-100, 100);
        goldImage.rectTransform.anchoredPosition += new Vector2(randomvalueX, randomvalueY);

        goldImage.transform.DOMove(TargetGoldImage.transform.position, 0.75f).SetEase(easeType).OnComplete(() =>
        {
            goldImage.gameObject.SetActive(false);
            GoldPrefabLists.Add(goldImage);
            if (!Effect.gameObject.activeSelf)
            {
                Effect.gameObject.SetActive(true);
            }
        });

        goldImage.transform.DORotate(new Vector3(0, 0, UnityEngine.Random.Range(-360, 360)), 0.75f, RotateMode.FastBeyond360).SetEase(Ease.Linear);

        goldImage.GetComponent<AudioSource>().Play();

    }

    public void RefreshProgress()
    {
        Number1.gameObject.SetActive(false);
        Number2.gameObject.SetActive(false);
        Number3.gameObject.SetActive(false);

        int score = BiliardGameManager.instance.CurrentScore;
        int score1 = score / 100;
        int score2 = score %100 / 10;
        int score3 = score % 10;
        if (score >= 999)
        {
            Number1.gameObject.SetActive(true);
            Number2.gameObject.SetActive(true);
            Number3.gameObject.SetActive(true);
            Number1.sprite = numberSprites[9];
            Number1.rectTransform.sizeDelta = new Vector2(numberSprites[9].texture.width, numberSprites[9].texture.height);

            Number2.sprite = numberSprites[9];
            Number2.rectTransform.sizeDelta = new Vector2(numberSprites[9].texture.width, numberSprites[9].texture.height);

            Number3.sprite = numberSprites[9];
            Number3.rectTransform.sizeDelta = new Vector2(numberSprites[9].texture.width, numberSprites[9].texture.height);
        }
        else
        {

            if (score1 == 0)
            {
                Number1.gameObject.SetActive(false);
                if (score2 == 0)
                {
                    Number2.gameObject.SetActive(false);
                }
                else
                {

                    Number2.gameObject.SetActive(true);
                    Number2.sprite = numberSprites[score2];
                    Number2.rectTransform.sizeDelta = new Vector2(numberSprites[score2].texture.width, numberSprites[score2].texture.height);
                }
                Number3.gameObject.SetActive(true);
                Number3.sprite = numberSprites[score3];
                Number3.rectTransform.sizeDelta = new Vector2(numberSprites[score3].texture.width, numberSprites[score3].texture.height);
            }
            else
            {
                Number1.gameObject.SetActive(true);
                Number1.sprite = numberSprites[score1];
                Number1.rectTransform.sizeDelta = new Vector2(numberSprites[score1].texture.width, numberSprites[score1].texture.height);

                Number2.gameObject.SetActive(true);
                Number2.sprite = numberSprites[score2];
                Number2.rectTransform.sizeDelta = new Vector2(numberSprites[score2].texture.width, numberSprites[score2].texture.height);

                Number3.gameObject.SetActive(true);
                Number3.sprite = numberSprites[score3];
                Number3.rectTransform.sizeDelta = new Vector2(numberSprites[score3].texture.width, numberSprites[score3].texture.height);
            }
        }

    }

    private Vector2 UpdateRectPosition(Vector3 EnemyTransform, Vector2 Offset)
    {
        Vector3 MyWorldPosition = EnemyTransform;
        Vector2 screenPosition = Camera.main.WorldToScreenPoint(MyWorldPosition);
        Vector2 localPosition;
        if (RectTransformUtility.ScreenPointToLocalPointInRectangle(rect, screenPosition, null, out localPosition))
        {
            return localPosition + Offset;
        }
        return Vector2.zero;
    }

    private void ScreenChangeCallBack(object sender, EventArgs ea)
    {
        if (Screen.width > Screen.height)
        {
            BG.anchoredPosition = new Vector2(0, -110);
            Effect.rectTransform.anchoredPosition = new Vector2(0, -110);
        }
        else
        {
            BG.anchoredPosition = new Vector2(0, -200);
            Effect.rectTransform.anchoredPosition = new Vector2(0, -200);
        }
    }

    public void RefreshBallGolds(int index,int targetValue) //球的序列，0，1，2三个球
    {
        RectTransform ballgold = null;
        ballgold = BallGolds[index];

        var e1 = ballgold.GetChild(1).GetComponent<Image>();
        var e2 = ballgold.GetChild(2).GetComponent<Image>();
        e1.gameObject.SetActive(false);
        e2.gameObject.SetActive(false);

        int score = targetValue;
        int score1 = score / 10;
        int score2 = score % 10;
        if (score >= 99)
        {
            e1.gameObject.SetActive(true);
            e2.gameObject.SetActive(true);
            e1.sprite = numberSprites[9];
            e1.rectTransform.sizeDelta = new Vector2(numberSprites[9].texture.width, numberSprites[9].texture.height);

            e2.sprite = numberSprites[9];
            e2.rectTransform.sizeDelta = new Vector2(numberSprites[9].texture.width, numberSprites[9].texture.height);
        }
        else
        {
            if (score1 != 0)
            {
                e1.gameObject.SetActive(true);
                e1.sprite = numberSprites[score1];
                e1.rectTransform.sizeDelta = new Vector2(numberSprites[score1].texture.width, numberSprites[score1].texture.height);

                e2.gameObject.SetActive(true);
                e2.sprite = numberSprites[score2];
                e2.rectTransform.sizeDelta = new Vector2(numberSprites[score2].texture.width, numberSprites[score2].texture.height);
            }
            else
            {
                e1.gameObject.SetActive(true);
                e1.sprite = numberSprites[score2];
                e1.rectTransform.sizeDelta = new Vector2(numberSprites[score2].texture.width, numberSprites[score2].texture.height);

                e2.gameObject.SetActive(false);
            }
        }
    }
}
